package mamikeji.liaowa.ui.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;

import java.util.List;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.ToDoBean;

/**
 * ExpandListView的适配器，继承自BaseExpandableListAdapter
 */
public class ToDoBaseExpandableListAdapter extends BaseExpandableListAdapter {
    private LayoutInflater inflater;
    private Context mContext;
    private List<ToDoBean.ListEntity> list;
    public static final int TYPE_0 = 0;
    public static final int TYPE_1 = 1;
    public static final int TYPE_2 = 2;
    public static final int TYPE_3 = 3;
    private OnItemClickListener mOnItemClickListener;
    private OnSeekBarChangeListener mOnSeekBarChangeListener;
    private OnCheckedChangeListener mOnCheckedChangeListener;

    public void setOnCheckedChangeListener(OnCheckedChangeListener mOnCheckedChangeListener) {
        this.mOnCheckedChangeListener = mOnCheckedChangeListener;
    }

    public interface OnCheckedChangeListener {
        void onCheckedChangeListener(RadioGroup radioGroup, int i);
    }

    public void setOnSeekBarChangeListener(OnSeekBarChangeListener mOnSeekBarChangeListener) {
        this.mOnSeekBarChangeListener = mOnSeekBarChangeListener;
    }

    public interface OnSeekBarChangeListener {
        void onSeekBarChangeListener(SeekBar seekBar, int i, int groupPosition, int childPosition);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onSingleItemClick(TodoChildSelectAdapter adapter, View view, int groupPosition, int position, int type);

        void onMultipleItemClick(TodoChildSelectAdapter adapter, View view, int groupPosition, int position, int type);

        void onToDoYesClick(View view, int groupPosition, boolean isExpanded);

        void onToDoNoClick(View view, int groupPosition, ToDoBean.ListEntity groupBean, boolean isExpanded);
    }

    public ToDoBaseExpandableListAdapter(Context context, List<ToDoBean.ListEntity> list) {
        this.mContext = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
    }

    /*
     *  Gets the data associated with the given child within the given group
     */
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        //我们这里返回一下每个item的名称，以便单击item时显示
        return list.get(groupPosition).getTask_optionsList().get(childPosition);
    }


    /*
     * 取得给定分组中给定子视图的ID. 该组ID必须在组中是唯一的.必须不同于其他所有ID（分组及子项目的ID）
     */
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    /*
     *  Gets a View that displays the data for the given child within the given group
     */
    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView,
                             ViewGroup parent) {
        final int type = list.get(groupPosition).getTType();
        if (type == 1) {
            convertView = inflater.inflate(R.layout.item_todo_childitem_1, parent, false);
            SeekBar seekBar_todo = (SeekBar) convertView.findViewById(R.id.seekBar_todo);
            TextView seekBar_min = (TextView) convertView.findViewById(R.id.seekBar_min);
            TextView seekBar_max = (TextView) convertView.findViewById(R.id.seekBar_max);
            final TextView seekBar_value = (TextView) convertView.findViewById(R.id.seekBar_value);
            final List<ToDoBean.ListEntity.Task_optionsListEntity> childList = list.get(groupPosition).getTask_optionsList();
            if (childList != null && childList.size() > 0) {
                seekBar_min.setText(childList.get(0).getTitle());
                seekBar_max.setText(childList.get(childList.size() - 1).getTitle());
                seekBar_todo.setMax(childList.size() - 1);

                int selectId = -1;
                for (int i = 0; i < childList.size(); i++) {
                    if (childList.get(i).getSelected() == 1) {
                        selectId = i;
                    }
                }

                if (selectId == -1) {
                    seekBar_todo.setProgress(0);
                    seekBar_value.setText(childList.get(0).getTitle());
                } else {
                    seekBar_todo.setProgress(selectId);
                    seekBar_value.setText(childList.get(selectId).getTitle());
                }

                seekBar_todo.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    @Override
                    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                        if (mOnSeekBarChangeListener != null) {
                            mOnSeekBarChangeListener.onSeekBarChangeListener(seekBar, i, groupPosition, childPosition);
                            seekBar_value.setText(childList.get(i).getTitle());
                            for (int j = 0; j< childList.size(); j++) {
                                if (i==j) {
                                    childList.get(j).setSelected(1);
                                }else{
                                    childList.get(j).setSelected(0);
                                }
                            }
                        }
                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {

                    }

                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {

                    }
                });
            }
        } else {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_todo_childitem_2, parent, false);
            RecyclerView recycleListView = (RecyclerView) convertView.findViewById(R.id.recycleListView_single);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
            if (type == 4) {
                linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            } else {
                linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            }
            recycleListView.setLayoutManager(linearLayoutManager);

//            final TodoChildSelectAdapter childSelectAdapter = new TodoChildSelectAdapter(mContext, list.get(groupPosition).getTask_optionsList());
//            recycleListView.setAdapter(childSelectAdapter);
//            childSelectAdapter.setOnItemClickListener(new TodoChildSelectAdapter.OnItemClickListener() {
//                @Override
//                public void onItemClick(View view, int position) {
//                    if (type == 3) {
//                        mOnItemClickListener.onMultipleItemClick(childSelectAdapter, view, groupPosition, position, type);
//                    } else {
//                        mOnItemClickListener.onSingleItemClick(childSelectAdapter, view, groupPosition, position, type);
//                    }
//
//                }
//            });
        }
        return convertView;
    }

    /*
     * 取得指定分组的子元素数
     */
    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    /**
     * 取得与给定分组关联的数据
     */
    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    /**
     * 取得分组数
     */
    @Override
    public int getGroupCount() {
        return list.size();
    }

    /**
     * 取得指定分组的ID.该组ID必须在组中是唯一的.必须不同于其他所有ID（分组及子项目的ID）
     */
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(final int groupPosition, final boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder groupHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_todo_groupitem, null);
            groupHolder = new GroupHolder();
            groupHolder.imageView_todo_yes = (ImageView) convertView.findViewById(R.id.imageView_todo_yes);
            groupHolder.imageView_todo_no = (ImageView) convertView.findViewById(R.id.imageView_todo_no);
            groupHolder.textView_todo_name = (TextView) convertView.findViewById(R.id.textView_todo_name);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (GroupHolder) convertView.getTag();
        }
        if (isExpanded) {
//            groupHolder.imageView_todo_yes.setImageResource(R.drawable.icon_yizuo_h);
        } else {
//            groupHolder.imageView_todo_yes.setImageResource(R.drawable.icon_yizuo_n);
        }
//        groupHolder.imageView_todo_no.setImageResource(R.drawable.icon_weizuo_n);
        final ToDoBean.ListEntity groupBean = list.get(groupPosition);
        groupHolder.textView_todo_name.setText(groupBean.getTitle());
        final GroupHolder finalGroupHolder = groupHolder;
        groupHolder.imageView_todo_yes.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onToDoYesClick(view, groupPosition, isExpanded);
            }
        });
        groupHolder.imageView_todo_no.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onToDoNoClick(view, groupPosition, list.get(groupPosition), isExpanded);
            }
        });

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    /**
     * 自定义接口，用于回调按钮点击事件到Activity
     *
     * @author Ivan Xu
     *         2014-11-26
     */
    public interface Callback {
        public void click(View v);
    }

    private class GroupHolder {
        ImageView imageView_todo_yes;
        ImageView imageView_todo_no;
        TextView textView_todo_name;
    }

    private class ChildHolder_1 {
        SeekBar seekBar_todo;
    }

    private class ChildHolder_2 {
        RecyclerView recycleListView_single;
    }

    private class ChildHolder_3 {
        RecyclerView recycleListView_multiple;
    }

    private class ChildHolder_4 {
        RadioGroup radioGroup_todo_yn;
        RadioButton radiobutton_todo_yes, radiobutton_todo_no;
    }

    public ToDoBean.ListEntity getGroupItem(int position) {
        return list.get(position);
    }

    public List<ToDoBean.ListEntity> getList() {
        return list;
    }
}
